package com.medic.web.controller;



import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.medic.entity.User;
import com.medic.service.impl.AssitantServiceImpl;
import com.medic.service.impl.DoctorServiceImpl;
import com.medic.service.impl.GroupServiceImpl;
import com.medic.service.impl.PharmacistServiceImpl;
import com.medic.service.impl.UserServiceImpl;




@RequestMapping("/web/members") 
@Controller 
public class MembersController {
	

	@Resource
	private AssitantServiceImpl assitantService;
	@Resource
	private PharmacistServiceImpl pharmacistService;
	@Resource
	private UserServiceImpl userService;


	@Resource
	private DoctorServiceImpl doctorService;

	@Resource 
	private GroupServiceImpl  groupService;
	
	
	
	
	@ResponseBody
	@RequestMapping("/action/{role}/remove/{group}")
	public Object exist(@RequestParam("id[]")int[] list, @PathVariable("group")int group ,@PathVariable("role")String role){
		Map<String,Object> object = new HashMap<String,Object>();
		try{
			switch(role){
			case "pharmacist" : groupService.pharmacistExitGroup(group, list);
			case "doctor" : groupService.doctorExitGroup(group, list);
			case "user" : groupService.userExitGroup(group, list);
			case "assitant" : groupService.assitantExitGroup( group,list);
			}
			}catch(Exception ee){
				ee.printStackTrace();
				object.put("status","error"); 
			}
			return object;
	}
	
	
	@ResponseBody
	@RequestMapping("/action/{role}/move/{group}")
	public Object enter(@RequestParam("id[]")int[] list,  @PathVariable("group")int group ,@PathVariable("role")String role){
		Map<String,Object> object = new HashMap<String,Object>();
		try{
			switch(role){
			case "pharmacist" : groupService.pharmacisBatchJoinGroup(list, group);
			case "doctor" : groupService.doctorBatchJoinGroup(list, group);
			case "user" : groupService.userBatchJoinGroup(list, group);
			case "assitant" : groupService.assitantBatchJoinGroup(list, group);
			}
			}catch(Exception ee){
				ee.printStackTrace();
				object.put("status","error");
			}
			return object;
	}
	

		
	
	@RequestMapping("data/{role}/notin/{group}")
	@ResponseBody
	public Object notin(@RequestParam("where")String where,@PathVariable()String role,@PathVariable("group")int group){
		Map<String,Object> object = new HashMap<String,Object>();
			
		
		try{
			
			switch(role){
				
			case "doctor":
				object.put("data",groupService.getDoctorNotinGroup(group, where));
				break;
			case "pharmacist" :
				object.put("data",groupService.getPharmacistNotinGroup(group, where));
				break;
			case "user":
				object.put("data",groupService.getUserNotinGroup(group,where));
				break;
			case "assitant":
				object.put("data", groupService.getAssitantNotinGroup(group, where));
				break;
			}
			
			
		}catch(Exception e){
			
		}
		
		
		
		return object;
	}
	
	@RequestMapping("index")
	public String index(Model model){
		
		try {
			
			
			model.addAttribute("groups" , groupService.getGroup(null));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		return "/web/MEMBERS";
	}
	
	
	@RequestMapping("/action/detail/{role}/{roleid}")
	public Object  detail(@PathVariable("role")String role, @PathVariable("roleid")int roleid, Model model){
		
		String result = "";
		try{
			
			switch(role){
				case "user": 
					model.addAttribute("role",userService.getUser(roleid));
					result = "/web/userinfo";
					break;
				case "doctor":
					model.addAttribute("role",doctorService.getDoctor(roleid));
					result = "/web/doctorinfo";
					break;
				case "assitant":
					model.addAttribute("role", assitantService.getAssitant(roleid));
					result = "/web/doctorinfo";
					break;
				case "pharmacist":
					model.addAttribute("role",pharmacistService.getPharmacist(roleid));
					result = "/web/doctorinfo";
					break;
			}	
			
		}catch(Exception  e){
			
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("data/{role}/{group}")
	public Object data(@RequestParam( required = false,value="pageno")Integer pageno,@PathVariable()String role ,@PathVariable()int group){
	    Map<String,Object> object = new HashMap<String,Object>();

		
		
		try{
			
			switch(role){
			
			case "doctor": 
				object.put("total",0);
				object.put("data" ,groupService.getDoctorByGroup(group));

				break;

			case "user": 
				Page<User> upage=groupService.getUserByGroup(group, pageno, null);
				object.put("data" ,upage);
				object.put("total",upage.getTotal());
				break;	
			
				
			case "assitant":  
				object.put("data" ,groupService.getAssitantByGroup(group));
				object.put("total",0);
				break;
			case "pharmacist": 
				object.put("total",0);
				object.put("data" ,groupService.getPharmacistByGroup(group));
				break;
			}
			
		}catch(Exception ee){
			
			
			
		}
		
		return object;
	}
	
	
	
	
	
	
	
	
}
